package jsave.init.xml;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Node;

public class JSaveDocumentXML {
	
	private static final Logger log = Logger.getLogger(JSaveDocumentXML.class);
	
	private Document doc;
	
	public JSaveDocumentXML(Document doc) {
		
		this.doc = doc;
	}

	public int getNbSauvegardes() {
		
		List nodes = doc.selectNodes("/jsave/save");
		
		log.debug("Nb balises 'save' : "+nodes.size());
		for (int i=0;i<nodes.size();i++) {
			Node node = ((Node)nodes.get(i));
			log.debug("Nom savegarde : "+node.valueOf("@nom"));
		}
		
		return nodes.size();
	}
	
	public String getNom(int i) {
		
		Node node = doc.selectSingleNode("/jsave/save["+i+"]");
		return node.valueOf("@nom");
	}
	
	public String getSource(int i) {
		
		Node node = doc.selectSingleNode("/jsave/save["+i+"]/source");
		return node.getText();
	}
	
	public String getDestination(int i) {
		
		Node node = doc.selectSingleNode("/jsave/save["+i+"]/destination");
		return node.getText();
	}
	
	public boolean isSimulation(int i) {
		
		try {
			Node node = doc.selectSingleNode("/jsave/save["+i+"]/simulation");
			if (node.getText().equalsIgnoreCase("true")) {
				return true;
			}
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		
		return false;
	}

	public boolean isRecursif(int i) {
		
		try {
			Node node = doc.selectSingleNode("/jsave/save["+i+"]/recursif");
			if (node.getText().equalsIgnoreCase("true")) {
				return true;
			}
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		
		return false;
	}
	
	public String getActionA(int i) {
		
		try {
			Node node = doc.selectSingleNode("/jsave/save["+i+"]/actionA");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		
		return null;
	}

	public String getActionB(int i) {
		
		try {
			Node node = doc.selectSingleNode("/jsave/save["+i+"]/actionB");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		
		return null;
	}

	public String getActionC(int i) {
		
		try {
			Node node = doc.selectSingleNode("/jsave/save["+i+"]/actionC");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		
		return null;
	}

	public String getActionD(int i) {
		
		try {
			Node node = doc.selectSingleNode("/jsave/save["+i+"]/actionD");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		
		return null;
	}
	
	public boolean isLogHTML(int i) {
		
		try {
			Node node = doc.selectSingleNode("/jsave/save["+i+"]/logHTML");
			if (node.getText().equalsIgnoreCase("true")) {
				return true;
			}
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		
		return false;
	}

	public String getCheminLog(int i) {
		
		try {
			Node node = doc.selectSingleNode("/jsave/save["+i+"]/cheminLog");
			return node.getText();
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
		
		return null;
	}
	
	public List getExclusions(int i) {
		
		List exclusions = new ArrayList();
		List nodes = doc.selectNodes("/jsave/save["+i+"]/exclusion");
		
		log.debug("Nb d'exclusions : "+nodes.size());
		for (int j=0;j<nodes.size();j++) {
			Node node = ((Node)nodes.get(j));
			exclusions.add(node.getText());
		}
		
		return exclusions;
	}
}